<!doctype html>
<html style="height:100%">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>启动页</title>

    <link rel="stylesheet" type="text/css" href="./css/api.css"/>

    <style>
        #bg {
            background-repeat: no-repeat;
            background-size: 100% 100%;
        }

        #div {
            display: none;
            position: absolute;
            right: 20px;
            top: 30px;
            background-color: rgba(0, 0, 0, 0.4);
            border-radius: 5px;
            height: 30px;
            line-height: 30px;
            padding: 0 10px;
            color: white;
            text-align: center;
        }

        #div span {
            line-height: 30px;
            height: 30px;
        }

        #div #count {
            color: #ffffff;
        }

        #progress {
            display: none;
            position: absolute;
            left: 50%;
            margin-left: -27px;
            bottom: 100px;
            width: 54px;
            height: 54px;
            line-height: 54px;
            border-radius: 50%;
            background-color: #33375E;
            border: 1px solid white;
            color: white;
            text-align: center;
        }

        #tips {
            display: none;
            position: absolute;
            width: 100%;
            height: 20px;
            text-align: center;
            bottom: 100px;
            left: 0;
            color: white;
        }

    </style>

</head>
<body style="height:100%;">
<div id="bg" style="width:100%;position:relative;" tapmode>
    <div id="progress">0%</div>
    <div id="tips">请下载最新版本!</div>
</div>
<div id="div" tapmode>
    跳过(<span id="count"></span>)
</div>
</body>
<script src="script/api.js"></script>
<script src="script/vue.js"></script>
<script src="script/app.js"></script>
<script src="script/common.js"></script>
<script src="script/url.js"></script>
<script>
    var startImg,
        _startImg;

    var image = null,
        _image = null;

    var bg = document.getElementById('bg');
    var div = document.getElementById('div');
    var count = document.getElementById('count');
    var number = 3;
    var t;

    var fs;
    var talkingData;
    var ajpush;
    var mq;

    var isStartImgNotExist;
    var canCountDown = true;
    var isAppear = true;
    apiready = function () {
        addListeners();
        fs = api.require('fs');
        talkingData = api.require('talkingData');
        ajpush = api.require('ajpush');
        api.requestPermission({
        list:['camera','storage']
        }, function(ret, err){
        });
        initModules();
        api.setStatusBarStyle({style: 'dark', color: ''});
        load();
        api.setAppIconBadge({
          badge: 0
      });
        // hotWords();

    };

    //下载广告图和更新app
    function load() {
        var t;
        if (api.systemType == 'ios') {
            t = 1;
        } else if (api.systemType == 'android') {
            t = 2;
        }
        var app_id = getApp_id();
        var data = {
            t: t,
            app_id: app_id,
            jpush_token: getPrefs('jpush_token') || ''
        };

        var callback = function (ret) {
            setPrefs('wap_url', ret.content.wap_url);
            setPrefs('forbid_urlschemes', ret.content.forbid_urlschemes);

            startImg = ret.content.splash_screen;
            intro_screens = ret.content.intro_screen;
            setPrefs('intro_screens', intro_screens);
            if (startImg) {
                image = startImg.image;
            }

            var im = new Image();
            im.onload = function () {
                bg.style.backgroundImage = 'url(' + im.src + ')';
                downloadStartImg(true);
            };
            if (getPrefs('startImg')) {
                _startImg = JSON.parse(getPrefs('startImg'));
                number = _startImg.wait_time?Number(_startImg.wait_time):3;
                _image = _startImg.image;
                fs.exist({
                    path: 'fs://startImg.png'
                }, function (ret) {
                    if (ret.exist) {
                        im.src = api.fsDir + '/startImg.png';
                    } else {
                        im.src = 'image/start_bg.png';
                        isStartImgNotExist = true;
                    }
                });

            } else {
                _startImg = null;
                im.src = 'image/start_bg.png';
            }

            downloadStartImg();

            //更新版本
            var version = ret.content.version;
            if (version) {
                api.confirm({
                    title: version.title,
                    msg: version.description,
                    buttons: ['取消', '马上更新']
                }, function (ret) {
                    if (ret.buttonIndex == 2) {
                        updateApp(version);
                    } else {
                        if (version.is_force == '0') {
                            advert();
                        } else {
                            api.closeWidget({
                                silent: true
                            });
                        }
                    }
                });
            } else {
                advert();
            }
        };
        var errback = function () {
            setTimeout(function () {
                api.removeLaunchView();
                ifLogin(null);
            }, 500)
        };

        pinet.post(APIURL+'/app/init/getBaseData', data, callback, errback);
    }

    //下载广告图
    function downloadStartImg(ifOnload) {
        if ((image && !_image) || (image && _image && image != _image && ifOnload) || image && _image && image == _image && isStartImgNotExist) {
            api.download({
                url: startImg.image,
                encode: false,
                savePath: 'fs://startImg.png',
                cache: false
            }, function (ret) {
                if (ret.state == 1) {
                    setPrefs('startImg', startImg);
                }
            });
        } else if (!image) {
            if (getPrefs('startImg')) {
                removePrefs('startImg');
            }
        }
    }

    function updateApp(version) {
        if (api.systemType == 'android') {
            var progress = document.getElementById('progress');
//            if (is_force) {
            progress.style.display = 'block';
            api.removeLaunchView();
//            }
            api.download({
                url: version.android_download,
                cache: false,
                allowResume: true,
                report: true
            }, function (ret) {
                progress.innerHTML = ret.percent + '%';
                if (ret.state == 1) {
                    api.installApp({
                        appUri: ret.savePath
                    });
                }
            });
        } else if (api.systemType == 'ios') {
            var tips = document.getElementById('tips');
//            if (is_force) {
            tips.style.display = 'block';
            api.removeLaunchView();
//            }
            // api.openApp({
            //     iosUrl: version.ios_download,
            // });
            api.openApp({
                iosUrl: 'https://zdapp.lawei-tech.com/iosdw.html',
            });
        }
    }

    function advert() {
        if (_startImg) {
            bg.onclick = jumpStartImg;
            div.style.display = 'block';
            div.onclick = function () {
                ifLogin(null);
            };
            api.parseTapmode();

            setTimeout(function () {
                api.removeLaunchView();
                countDown();
            }, 500);
        } else {
            setTimeout(function () {
                api.removeLaunchView();
                ifLogin(null);
            }, 500)
        }
    }

    function countDown() {
        if (!canCountDown) {
            return;
        }

        count.innerHTML = number;
        t = setTimeout(function () {
            if (number > 1) {
                number--;
                countDown();
            } else {
                clearTimeout(t);
                ifLogin();
            }
        }, 1000)
    }

    function ifLogin(params) {
        if (t) {
            clearTimeout(t);
        }
        params = params || null;
        if (hasPrefs('token')) {

			      openWin('index', params, {slidBackEnabled:false});
        } else {

            openWin('html/login/login', params, {slidBackEnabled:false});

        }
    }

    function hotWords() {
        pinet.get(URL.getHotKeys, function (data) {
            setPrefs('hotWords', data.content);
        });
    }

    function jumpStartImg() {
        ifLogin({startImg: _startImg});
    }

    function addListeners() {
        api.addEventListener({
            name: 'appintent'
        }, function (ret) {
            if (ret.appParam) {
                var param = appParamToJson(ret.appParam);
                if (param.goods_id) {
                    if (isAppear) {
                        canCountDown = false;
                        ifLogin({goods_id: param.goods_id});
                    } else {
                        openWin('widget://html/home/browser', {goods_id: param.goods_id});
                    }
                }
            }
        });
    }

    function initModules() {
        //缓存大于100MB时清除
        var size;
        api.getCacheSize(function (ret) {
                size = ((ret.size / 1024) / 1024).toFixed(1);
                if (size > 100) {
                    api.clearCache(function () {
                        api.toast({
                            msg: '清除完成'
                        });
                    });
                }
            }
        );

        // var zyBaiduFace = api.require('zyBaiduFace');
        // var param = {
        //   "licenseID":api.systemType=="ios"?'quhongr-face-ios':'quhongr-face-android',
        //   "licenseFileName":api.systemType=="ios"?'quhongr-license.face-ios':'quhongr-license.face-android',
        //   "groupID":"qhr",
        //   "apiKey":"qwFsMfDoSn6WOGn68CXcLpn6",
        //   "secretKey":"yT9f8HmrEBgqQqiu2m3Sj5OmzRyWRmmP",
        // };
        // zyBaiduFace.init(param, function(ret, err){
        // });

    //     var webBrowser = api.require('webViewModule');
    // webBrowser.initSDK(function (ret) {
    // });


        // if (api.systemType == 'android') {
        //     ajpush.init(function (ret) {
        //         if (ret && ret.status) {
        //             getRegistrationId();
        //         } else {
        //             vm.login('');
        //         }
        //     });
        // } else {
        //     getRegistrationId();
        // }

        // mq = api.require('meiQia');
        // var param = {appkey: 'a72c59ce3c73599450c14f0b466394e2'};
        // mq.initMeiQia(param, function () {
        // });
    }

    function getRegistrationId() {
        ajpush.getRegistrationId(function (ret) {
            if (ret) {
                var registrationId = ret.id;
                setPrefs('jpush_token', registrationId);
            }
        });
    }

    function getApp_id() {
        var appVersion;
        if(api.systemType == 'android'){
            appVersion = api.appVersion;
        }else if(api.systemType == 'ios'){
            var appVersionModule = api.require('appVersionModule');
            appVersion = appVersionModule.getBuild().app_build;
        }
        var arr = appVersion.split('.');
        for (var i = 1; i < arr.length; i++) {
            var _arr = arr[i].split('');
            arr[i] = _arr[1] ? arr[i] : '0' + arr[i];
        }
        return arr.join('');
    }

</script>
</html>
